package com.nativetalk.controller.app.three.reserve;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.reserve.TdNtReserveInsert;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.three.reserve.insert.ThreeReserveInsertService;
import com.nativetalk.service.three.reserve.search.ThreeReserveSearchService;
import com.nativetalk.service.three.reserve.time.search.ThreeReserveTimeSearchService;
import com.nativetalk.service.three.reserve.update.ThreeReserveUpdateService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 * Modified by wangzheng on 17/02/15
 */
@Controller
public class ThreeReserveController {
    private static Logger logger = Logger.getLogger(ThreeReserveController.class);
    @Resource
    private ThreeReserveInsertService threeReserveInsertService;
    @Resource
    private ThreeReserveTimeSearchService threeReserveTimeSearchService;
    @Resource
    private ThreeReserveSearchService threeReserveSearchService;
    @Resource
    private ThreeReserveUpdateService threeReserveUpdateService;

    /**
     * @return RetInfo
     * @Purpose 预约
     * @version 3.1
     * @time    2015-07-19
     * @author lizhun
     */
    @RequestMapping(value = "/app/7/token/reserve/insert", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo insert(HttpServletRequest request, @RequestBody TdNtReserveInsert tdNtReserveInsert,
                           @RequestHeader(value = "token", required = false) String token,
                          @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtReserveInsert));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            tdNtReserveInsert.setMemb_id(tdNtMember.getMemb_id());
            retInfo = threeReserveInsertService.addTdNtReserveInsert(tdNtReserveInsert, version, 1, tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 取消预约()
     * @version 4.1
     * @time    2015-07-19
     * @author lizhun
     * @modifider wangzheng
     * @time 2017-02-23
     */
    @RequestMapping(value = "/app/7/token/teacher/reserve/cancel", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo cancel(@RequestBody Map<String,Integer> map,
                         @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = threeReserveUpdateService.cancelByMembId(map.get("reserve_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 不可取消的未完成预约列表
     * @version 4.1
     * @time    2015-07-19
     * @author lizhun
     * @modifider wangzheng
     * @time 2017-02-23
     */
    @RequestMapping(value = "/app/7/token/teacher/reserve/list/unfinish", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo unfinish(@RequestBody Page page,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            page.setObj(tdNtMember.getMemb_id());
            retInfo = threeReserveSearchService.findTdNtReserveUnFinishByMembId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 未完成预约列表
     * @version 4.1
     * @time    2015-07-19
     * @author lizhun
     * @modifider wangzheng
     * @time 2017-02-23
     */
    @RequestMapping(value = "/app/7/token/teacher/reserve/list/finish", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo finish(@RequestBody Page page,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            page.setObj(tdNtMember.getMemb_id());
            retInfo = threeReserveSearchService.findTdNtReserveFinishByMembId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}



